<?php
namespace app\admin\controller;

use think\Db;
use app\admin\model\RegcompanyModel;
use app\admin\model\ReggovernmentModel;

class Users extends Base {
	/**
	 * [村账号列表]
	 */
	public function VillageList() {
		if($this->request->isPost()){
			$param = input("post.");
			$limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$where['level'] = 1;
			$RegGovernmentModel = new ReggovernmentModel();
			$return['rows'] = $RegGovernmentModel->getListRows($where,$offset,$limit);
			foreach($return['rows'] as $k => $v){
				$return['rows'][$k]['register_status'] = DB::name('user')->where('id',$v['uid'])->value('register_status');
				if($return['rows'][$k]['register_status'] == 2){
					$return['rows'][$k]['register_status_name'] = "已通过";
				}else{
					$return['rows'][$k]['register_status_name'] = "待审核";
				}
			}
			$return['total'] = $RegGovernmentModel->getCount($where);
			return ['code'=>1,'msg'=>'表格加载成功','data'=>$return];
		}
		return $this->fetch();
	}
	/**
	 * [镇账号列表]
	 */
	public function TownList() {
		if($this->request->isPost()){
			$param = input("post.");
			$limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$where['level'] = 2;
			$RegGovernmentModel = new ReggovernmentModel();
			$return['rows'] = $RegGovernmentModel->getListRows($where,$offset,$limit);
			foreach($return['rows'] as $k => $v){
				$return['rows'][$k]['register_status'] = DB::name('user')->where('id',$v['uid'])->value('register_status');
				if($return['rows'][$k]['register_status'] == 3){
					$return['rows'][$k]['register_status_name'] = "已通过";
				}else{
					$return['rows'][$k]['register_status_name'] = "待审核";
				}
			}
			$return['total'] = $RegGovernmentModel->getCount($where);
			return ['code'=>1,'msg'=>'表格加载成功','data'=>$return];
		}
		return $this->fetch();
	}
	/**
	 * [县账号列表]
	 */
	public function CountyList() {
		if($this->request->isPost()){
			$param = input("post.");
			$limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$where['level'] = 3;
			$RegGovernmentModel = new ReggovernmentModel();
			$return['rows'] = $RegGovernmentModel->getListRows($where,$offset,$limit);
			foreach($return['rows'] as $k => $v){
				$return['rows'][$k]['register_status'] = DB::name('user')->where('id',$v['uid'])->value('register_status');
				if($return['rows'][$k]['register_status'] == 4){
					$return['rows'][$k]['register_status_name'] = "已通过";
				}else{
					$return['rows'][$k]['register_status_name'] = "待审核";
				}
			}
			$return['total'] = $RegGovernmentModel->getCount($where);
			return ['code'=>1,'msg'=>'表格加载成功','data'=>$return];
		}
		return $this->fetch();
	}
	/**
	 * [州账号列表]
	 */
	public function StateList() {
		if($this->request->isPost()){
			$param = input("post.");
			$limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$where['level'] = 4;
			$RegGovernmentModel = new ReggovernmentModel();
			$return['rows'] = $RegGovernmentModel->getListRows($where,$offset,$limit);
			foreach($return['rows'] as $k => $v){
				$return['rows'][$k]['register_status'] = DB::name('user')->where('id',$v['uid'])->value('register_status');
				if($return['rows'][$k]['register_status'] == 5){
					$return['rows'][$k]['register_status_name'] = "已通过";
				}else{
					$return['rows'][$k]['register_status_name'] = "待审核";
				}
			}
			$return['total'] = $RegGovernmentModel->getCount($where);
			return ['code'=>1,'msg'=>'表格加载成功','data'=>$return];
		}
		return $this->fetch();
	}
	/**
	 * [企业账号列表]
	 */
	public function CompanyList() {
		if($this->request->isPost()){
			$param = input("post.");
			$limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$where = [];
			$RegCompanyModel = new RegcompanyModel();
			$return['rows'] = $RegCompanyModel->getListRows($where,$offset,$limit);
			foreach($return['rows'] as $k => $v){
				$return['rows'][$k]['register_status'] = DB::name('user')->where('id',$v['uid'])->value('register_status');
				if($return['rows'][$k]['register_status'] == 1){
					$return['rows'][$k]['register_status_name'] = "已通过";
				}else{
					$return['rows'][$k]['register_status_name'] = "待审核";
				}
			}
			$return['total'] = $RegCompanyModel->getCount($where);
			return ['code'=>1,'msg'=>'表格加载成功','data'=>$return];
		}
		return $this->fetch();
	}

	/**
	 * [通过user_id获取县]
	 */
	public function getDepart($uid){
		$depart = DB::name('department')->alias('depart')
					->join('role role','depart.id = role.department_id')
					->join('user user','user.role_id = role.id')
					->where('user.id',$uid)
					->field('depart.name,depart.id')->find();
		return $depart;
	}

	/**
	 * [注册]
	 */
	public function Register() {
		$uid = input('get.uid');
		$type = input('get.type');// 1.企业 2.政府
		$depart = $this->getDepart($uid);
		if($type == 2){
			$this->assign([
				'uid' => $uid,
				'type' => 2,
				'depart' =>$depart,
				'user' => DB::name("user")->where("id",$uid)->find()
			]);
			return $this->fetch("becomegovernment");
		}else{
			$this->assign([
				'uid' => $uid,
				'user' => DB::name("user")->where("id",$uid)->find(),
				'depart' => $depart
			]);
			return $this->fetch("becomecompany");
		}
	}
	/**
	 * [获取商品类别树]
	 */
	public function getGoodsCateTree() {
		$arr = DB::name("goods_cate")->field("id,pid,name,commid,commid_2")->select();
		$data = $this->arr2Tree($arr);
		return ['code'=>1,'msg'=>'','data'=>$data];
	}
	/**
	 * [注册成企业]
	 */
	public function becomeCompany() {
		$param = input('post.');
		$check = DB::name('users_company')->where('uid',$param['uid'])->count();
		if($check != 0){
			return ['code'=>0,'msg'=>'注册失败,注册信息已提交'];
		}
		foreach($param['buy'] as $k => $v){
			if($v == 0){
				unset($param['buy'][$k]);
			}
		}
		foreach($param['sell'] as $k => $v){
			if($v == 0){
				unset($param['sell'][$k]);
			}
		}
		if($param['buy']){
			$param['buy'] = implode(",",$param['buy']);
		}
		if($param['sell']){
			$param['sell'] = implode(",",$param['sell']);
		}
		$inData = $param;
		unset($inData['linkMan']);
		$inData['link_man'] = $param['linkMan'];
		unset($inData['legalPerson']);
		$inData['legal_person'] = $param['legalPerson'];
		unset($inData['plWebsite']);
		$inData['pl_website'] = $param['plWebsite'];
		unset($inData['appUrl']);
		$inData['app_url'] = $param['appUrl'];
		$inData['create_uid'] = $param['uid'];
		DB::startTrans();
		try{
			DB::name('users_company')->insert($inData);
			DB::name("user")->where('id',$inData['uid'])->update([
				'register_status' => '71'
			]);
			DB::commit();
			return ['code'=>1,'msg'=>'注册成功，等待审核'];
		}catch(\Exception $e){
			DB::rollback();
			return ['code'=>0,'msg'=>'注册失败:'.$e->getMessage()];
		}
	}
	/**
	 * [审核企业注册]
	 */
	public function exCompany() {
		$param = input("post.");
		DB::startTrans();
		try{
			if($param['state'] == 1){
				# 审核通过
				# 1.修改user表状态
				DB::name("user")->where("id",$param['uid'])->update([
					'register_status' => '1'
				]);
				$msg = "审核通过操作成功";
			}else{
				# 审核不通过
				# 1.修改user表状态
				# 2.删除users_company表
				DB::name("user")->where("id",$param['uid'])->update(['register_status' => '0']);
				DB::name("users_company")->where("id",$param['row_id'])->delete();
				$msg = "审核不通过操作成功，已打回重新注册";
			}
			DB::commit();
			return ['code'=>1,'msg'=>$msg];
		}catch(\Exception $e){
			DB::rollback();
			return ['code'=>1,'msg'=>'审核操作失败:'.$e->getMessage()];
		}
	}

	/**
	 * [审核县注册]
	 */
	public function exGovernment() {
		$param = input("post.");
		DB::startTrans();
		try{
			if($param['state'] == 1){
				# 审核通过
				# 1.修改user表状态
				$reg_status = $this->getRegStatusByLevel($param['level']);
				DB::name("user")->where("id",$param['uid'])->update([
					'register_status' => $reg_status
				]);
				$msg = "审核通过操作成功";
			}else{
				# 审核不通过
				# 1.修改user表状态
				# 2.删除users_company表
				DB::name("user")->where("id",$param['uid'])->update(['register_status' => '0']);
				DB::name("users_government")->where("id",$param['row_id'])->delete();
				$msg = "审核不通过操作成功，已打回重新注册";
			}
			DB::commit();
			return ['code'=>1,'msg'=>$msg];
		}catch(\Exception $e){
			DB::rollback();
			return ['code'=>1,'msg'=>'审核操作失败:'.$e->getMessage()];
		}
	}

	public function getRegStatusByLevel($level) {
		switch ($level) {
			case '1':
				$reg_status = 2;break;
			case '2':
				$reg_status = 3;break;
			case '3':
				$reg_status = 4;break;
			case '4':
				$reg_status = 5;break;
			default:
				$reg_status = 0;break;
		}
		return $reg_status;
	}

	/**
	 * [注册县账号]
	 */
	public function becomeCounty() {
		$param = input('post.');
		$check = DB::name('users_government')->where('uid',$param['uid'])->count();
		if($check != 0){
			return ['code'=>0,'msg'=>'注册失败,注册信息已提交'];
		}
		$inData = $param;
		unset($inData['linkType']);
		$inData['link_type'] = $param['linkType'];
		$inData['create_uid'] = $param['uid'];
		DB::startTrans();
		try{
			DB::name('users_government')->insert($inData);
			DB::name("user")->where('id',$inData['uid'])->update([
				'register_status' => '74'
			]);
			DB::commit();
			return ['code'=>1,'msg'=>'注册成功，等待审核'];
		}catch(\Exception $e){
			DB::rollback();
			return ['code'=>0,'msg'=>'注册失败，'.$e->getMessage()];
		}
	}
}
